package com.meitianhui.convenitenbuy.module.store.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.store.contract.StoreSearchContract;
import com.meitianhui.convenitenbuy.module.store.model.StoreInfo;
import com.meitianhui.convenitenbuy.module.store.ui.activity.StoreSearchActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amy on 2017/11/29.
 */

public class StoreSearchPresenter implements StoreSearchContract.Presenter {

    private StoreSearchContract.View mView;
    private StoreSearchActivity mContext;

    public StoreSearchPresenter(StoreSearchActivity activity,
                                StoreSearchActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getDefaultStore() {
        Map<String, String> map = new HashMap<>();
        map.put("longitude", InfoProvider.getLongitude(mContext));
        map.put("latitude", InfoProvider.getLatitude(mContext));

        MineApiDataSource.getInstance()
                .getDefaultSearchStore(InfoProvider.getAppToken(mContext),
                        "member.nearbyAllStoreFindForHomePage", new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<StoreInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<StoreInfo> response) {
                        List<StoreInfo.Convenient> list = response.data.getList();
                        if (list.size() > 0) {
                            mView.getDefaultStoreSucc(response.data);
                        } else {
                            // 为空和
                            mView.showEmpty();
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

    @Override
    public void getSearchStore(String s, int pageIndex, int pageSize) {
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageIndex + "");
        maps.put("page_size", pageSize + "");
        Map<String, String> map = new HashMap<>();
        map.put("stores_like", s);

        MineApiDataSource.getInstance()
                .getSearchStores(InfoProvider.getAppToken(mContext),
                        "member.storeListPageFind", new Gson().toJson(maps), new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<StoreInfo>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<StoreInfo> response) {
                        List<StoreInfo.Convenient> convenientList = response.data.getList();
                        if (convenientList.size() > 0) {
                            mView.getSearchStoreSucc(response.data);
                        } else {
                            if (pageIndex == 1) {
                                mView.showEmpty();
                            }
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

}
